<?php

namespace backend\modules\business\models;

use common\models\business\BusinessInspection;
use common\models\sys\SysSetting;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\business\BusinessPig;
use yii\db\Query;

/**
 * BusinessPigSearch represents the model behind the search form about `common\models\business\BusinessPig`.
 */
class BusinessPigSearch extends BusinessPig
{
    public $isinspection;
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id','varietyid', 'beforevaccinetime', 'aftervaccinetime', 'intime', 'livery', 'status', 'fileid','canlivery'], 'integer'],
            [['no','unino','headpersonid','cost', 'price', 'batch','planout', 'gender', 'father', 'mathor', 'photo', 'screen_photo','isinspection'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = BusinessPig::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC,
                ]
            ],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        //隐藏预制二维码的仔猪
        if(SysSetting::getValue('isshowprepig') == 1){
            $query->andWhere(['NOT', ['varietyid' => 0]])
                    ->andWhere(['NOT', ['fileid' => 0]]);
        }

        //现在距离一个巡检周期的时间点
        $difftime = time()-(SysSetting::getValue('inspection_cycle')*24*3600);
        //已巡检
        if($this->isinspection == 2){
            $subQuery = (new Query())->select('id')
                ->from(BusinessInspection::tableName().' as ins')
                ->where('ins.pigid=business_pig.id and ins.createtime>:dtime',[':dtime'=>$difftime]);
            $query->andFilterWhere(['exists', $subQuery]);
        }else if($this->isinspection == 1){
            $subQuery = (new Query())->select('id')
                ->from(BusinessInspection::tableName().' as ins')
                ->where('ins.pigid=business_pig.id and ins.createtime>:dtime',[':dtime'=>$difftime]);
            $query->andFilterWhere(['not exists', $subQuery]);
        }


        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'varietyid' => $this->varietyid,
            'beforevaccinetime' => $this->beforevaccinetime,
            'aftervaccinetime' => $this->aftervaccinetime,
            'intime' => $this->intime,
            'livery' => $this->livery,
            'status' => $this->status,
            'fileid' => $this->fileid,
            'cost' => $this->cost,
            'price' => $this->price,
            'headpersonid' => $this->headpersonid,
            'canlivery' => $this->canlivery,
        ]);

        if(!empty($this->planout)){
            $date = explode(' - ',$this->planout);
            $query->andFilterWhere(['between', 'planout', strtotime($date[0]), strtotime($date[1])]);
        }

        $query->andFilterWhere(['like', 'no', $this->no])
            ->andFilterWhere(['like', 'unino', $this->unino])
            ->andFilterWhere(['like', 'weight', $this->weight])
            ->andFilterWhere(['like', 'height', $this->height])
            ->andFilterWhere(['like', 'batch', $this->batch])
            ->andFilterWhere(['like', 'gender', $this->gender])
            ->andFilterWhere(['like', 'father', $this->father])
            ->andFilterWhere(['like', 'mathor', $this->mathor])
            ->andFilterWhere(['like', 'photo', $this->photo])
            ->andFilterWhere(['like', 'screen_photo', $this->screen_photo]);

        return $dataProvider;
    }
}
